package com.wizz.trade.template.generator;

import com.alibaba.fastjson.JSON;
import com.wizz.trade.excel.ExcelBuildParams;
import com.wizz.trade.template.core.ITemplateHandler;
import com.wizz.trade.template.entity.PCDetails;
import com.wizz.trade.template.entity.dto.PurchaseContractInfoDto;
import com.wizz.trade.template.entity.service_document.PurchaseContractInfo;
import com.wizz.trade.template.entity.service_document.Party;
import com.wizz.trade.template.entity.Template;
import com.wizz.trade.template.mapper.TemplateMapper;
import com.wizz.trade.util.ExcelUtil;
import com.wizz.trade.util.NumberToCN;
import org.springframework.beans.BeanUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;



@Component
public class PurchaseContractHandler extends Template implements ITemplateHandler {

    @Resource
    private ResourceLoader resourceLoader;


    @Resource
    private TemplateMapper templateMapper;


    @Override
    public Template doGenerate() {

        PurchaseContractInfoDto template = new PurchaseContractInfoDto();

        ArrayList<String> clauses = templateMapper.getClausesByTemplateId(1);

        template.setId(1);
        template.setName("采购-合同模版.xlsx");
        template.setCategoryId(1);
        template.setFileSrc("template/service_document/test.xlsx");

        template.setClauses(clauses);
        template.setDate(LocalDateTime.now().toString());
        template.setFirstParty(new Party("中投", null, null, null));
        template.setSecondParty(new Party(null, null, null, null));


        return template;
    }

    @Override
    public void doExport(HttpServletResponse resp, Map<String, Object> params) {

        PurchaseContractInfoDto infoDto = JSON.parseObject(JSON.toJSONString(params), PurchaseContractInfoDto.class);

        PurchaseContractInfo info = new PurchaseContractInfo();

        BeanUtils.copyProperties(infoDto, info);
        info.setFirstPartyName(infoDto.getFirstParty().getName());
        info.setFirstPartyProxy(infoDto.getFirstParty().getProxy());
        info.setFirstPartyFax(infoDto.getFirstParty().getFax());
        info.setFirstPartyPhone(infoDto.getFirstParty().getPhone());
        info.setSecondPartyName(infoDto.getSecondParty().getName());
        info.setSecondPartyProxy(infoDto.getSecondParty().getProxy());
        info.setSecondPartyFax(infoDto.getSecondParty().getFax());
        info.setSecondPartyPhone(infoDto.getSecondParty().getPhone());

        Map infoMap = JSON.parseObject(JSON.toJSONString(info), Map.class);

        //1. 计算数据（假装从数据库查询，伪造一些）
        PCDetails details1 = new PCDetails(1, "品种1", "商标1", "K30", "上海",
                BigDecimal.valueOf(34.0), 100, 100, BigDecimal.valueOf(3400.0), 1);
        PCDetails details2 = new PCDetails(2, "品种2", "商标2", "A49", "西安",
                BigDecimal.valueOf(42.0), 100, 100, BigDecimal.valueOf(4200.0), 1);
        PCDetails details3 = new PCDetails(3, "品种3", "商标3", "D90", "理塘",
                BigDecimal.valueOf(34.0), 100, 100, BigDecimal.valueOf(3400.0), 1);
        PCDetails details4 = new PCDetails(4, "品种4", "商标4", "K37", "上海",
                BigDecimal.valueOf(32.0), 100, 100, BigDecimal.valueOf(3200.0), 1);
        PCDetails details5 = new PCDetails(5, "品种1", "商标1", "P44", "江苏",
                BigDecimal.valueOf(67.0), 100, 100, BigDecimal.valueOf(6700.0), 1);
        List<PCDetails> data = new ArrayList<>();
        data.add(details1);
        data.add(details2);
        data.add(details3);
        data.add(details4);
        data.add(details5);

        BigDecimal total = BigDecimal.ZERO;
        for (PCDetails pcDetails : data) {
            total = total.add(pcDetails.getMoney());
        }
        infoMap.put("total", total);
        String totalToCN = NumberToCN.numberToCn(total);
        infoMap.put("totalToCN", totalToCN);


        StringBuffer clauseBuffer = new StringBuffer();
        for (String clause : infoDto.getClauses()) {
            clauseBuffer.append(clause + "\n");
        }
        infoMap.put("clauses", clauseBuffer.toString());


        //2. 封装参数
        ExcelBuildParams params0 = new ExcelBuildParams();
        params0.setFileName(info.getName());
        params0.setMap(infoMap);
        params0.setData(data);
        params0.setForceNewRow(Boolean.TRUE);





            ClassPathResource classPathResource = new ClassPathResource("template/service_document/采购-合同模板.xlsx");
        try {
            InputStream inputStream = classPathResource.getInputStream();
            params0.setInputStream(inputStream);

        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        // help test
            System.out.println("===============================================================================================================");
            System.out.println();
            System.out.println("===============================================================================================================");




        //3. 生成excel
        ExcelUtil.exportExcelWithLocalTemplate(resp, params0);


    }
}
